#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
    vector<int> ans;
    map<int, int> mp;
    stack<TreeNode *> stk;
    vector<int> findMode(TreeNode *root)
    {
        TreeNode *ptr = NULL;
        int mxcnt = -1;
        stk.emplace(root);
        while (stk.empty() == false)
        {
            ptr = stk.top();
            stk.pop();
            if (ptr == NULL)
                continue;
            mp[ptr->val]++;
            mxcnt = max(mxcnt, mp[ptr->val]);
            stk.emplace(ptr->left);
            stk.emplace(ptr->right);
        }
        for (auto &it : mp)
        {
            if (it.second == mxcnt)
                ans.push_back(it.second);
        }
        return ans;
    }
};